package fr.mywiki.view.web.struts.form.entry;


import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.ejb.FinderException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import fr.mywiki.business.account.AccountLite;
import fr.mywiki.business.entry.EntryAccessManager;
import fr.mywiki.business.entry.EntryLite;
import fr.mywiki.business.entry.EntryManager;
import fr.mywiki.business.login.LoginManager;
import fr.mywiki.business.profil.Profile;
import fr.mywiki.business.versioning.VersionLite;
import fr.mywiki.business.versioning.VersionManager;
import fr.mywiki.business.wiki.render.WikiRenderEngine;

/**
 * The Action Form for the formular which displays an entry.
 * 
 * @author tlombard
 */
public class EntryPageForm extends ActionForm {
	private static final long serialVersionUID = 2L;
	
	/** The entry linked to the current form */
	private EntryLite entry;

	/** The entry's title */
	private String title;
	
	/** The entry content */
	private String content;
	
	/** The title of the entry to create */
	private String entryTitle;
	
	/** The identifier of the version currently displayed */
	private Long versionId;
	
	/** Do we display display the message telling the user this is not the current version of the entry ? */
	private boolean displayMessage;
	
	/** Is the current user allowed to edit this entry ? */
	private boolean canEditEntry;
	
	/** Is the current user allowed to delete the entry ? */
	private boolean canDeleteEntry;
	
	/**
	 * Initializes the form with a specific entry. Displays in this case the
	 * current version.
	 * 
	 * @param entry a <code>EntryLite</code> object
	 * @param request the request
	 */
	public void init(EntryLite entry, HttpServletRequest request) throws RemoteException, FinderException, SQLException {
		/* The content is wiki-rendered before */
		VersionLite version = EntryManager.getCurrentVersion(entry);
		init0(version, entry, LoginManager.getLoggedUser(request.getSession()));
	}
	
	/**
	 * What is always initialized in the form.
	 * 
	 * @param version
	 *          the version to display
	 * @param entry
	 *          the entry to display
	 * @param account the current user
	 * @throws SQLException 
	 */
	private void init0(VersionLite version, EntryLite entry, AccountLite account)
			throws RemoteException, FinderException, SQLException {
		/* The content is wiki-rendered before */
		content = WikiRenderEngine.render(VersionManager.getInstance()
				.getVersionContent(version));

		this.entry = entry;
		versionId = version.getId();
		title = (String) entry.get(EntryLite.FIELD_TITLE);

		entryTitle = new String();
		
		displayMessage = !VersionManager.getInstance().isCurrentVersion(version);
		
		canEditEntry = EntryAccessManager.canEditEntry(account,entry);
		
		/** Only the administrator can delete an entry */
		canDeleteEntry = account!=null && Profile.ADMIN.equals(account.getProfile());
	}
	
	/**
	 * Initializes the form with a specific version of an entry.
	 * 
	 * @param version a <code>VersionLite</code> object
	 * @param request the request
	 */
	public void init(VersionLite version,  HttpServletRequest request) throws RemoteException, FinderException, SQLException {
		EntryLite entry = VersionManager.getInstance().getEntry(version);
		init0(version, entry, LoginManager.getLoggedUser(request.getSession()));
	}
	
	public String getContent() {
		return content;
	}

	public String getTitle() {
		return title;
	}

	public String getEntryTitle() {
		return entryTitle;
	}

	public void setEntryTitle(String entryTitle) {
		this.entryTitle = entryTitle;
	}

	public Long getVersionId() {
		return versionId;
	}

	public void setVersionId(Long a_versionId) {
		versionId = a_versionId;
	}

	public boolean isDisplayMessage() {
		return displayMessage;
	}

	public void setDisplayMessage(boolean a_displayMessage) {
		displayMessage = a_displayMessage;
	}

	public boolean isCanEditEntry() {
		return canEditEntry;
	}

	public void setCanEditEntry(boolean a_canEditEntry) {
		canEditEntry = a_canEditEntry;
	}

	public boolean isCanDeleteEntry() {
		return canDeleteEntry;
	}

	public void setCanDeleteEntry(boolean a_canDeleteEntry) {
		canDeleteEntry = a_canDeleteEntry;
	}

	public EntryLite getEntry() {
		return entry;
	}

	public void setEntry(EntryLite a_entry) {
		entry = a_entry;
	}
}
